package com.base.controller;

import java.io.PrintWriter;
import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.base.comm.CommContent;
import com.base.po.voip.Smsconfigs;
import com.base.po.voip.Users;
import com.base.service.FreeroutesService;
import com.base.service.LogsService;
import com.base.service.SmsconfigsService;
import com.base.service.UsersService;

@Controller
@RequestMapping("/admin")
public class content_chongzhi {
	@Autowired
	private LogsService logsService;// log日志
	@Autowired
	private UsersService usersService;
	@Autowired
	private FreeroutesService freeroutesService;
	@Autowired
	private SmsconfigsService smsconfigsService;

	// 账户信息
	@RequestMapping("/content_chongzhi")
	public ModelAndView index(HttpServletRequest request, @RequestParam(value = "id", required = true) String id,
			Integer choose) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		if (id == null) {
			modelAndView.setViewName("jsp/datanotfound");
			return modelAndView;
		}
		// 获取登陆ID
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		Users users = usersService.selectByPrimaryKey(id);
		if (users == null || users.getId() == null) {
			modelAndView.setViewName("jsp/datanotfound");
			return modelAndView;
		}
		
		// TODO 代理商信息
		Users pUsers = usersService.selectByPrimaryKey(users.getParentid());
		// TODO 上级商家信息
		Users sUsers = usersService.selectByPrimaryKey(users.getShopid());
		// 设置页面进行跳转
		request.setAttribute("id", id);
		
		if (request.getSession().getAttribute("adminUserId") == null) {
			modelAndView.setViewName("jsp/login");
		} else {
		
		  modelAndView.setViewName("jsp/content_chongzhi");
			
		}
		return modelAndView;
	}

	String SUCCESS = "success";

	// 商户充值
	@RequestMapping(value = "/updateOemUsers", method = RequestMethod.POST)
	public void updateUsers(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "id", required = true) String id,
			BigDecimal linebalance,// 充值线路余额
			Integer overdraft,// 设置透支金额
			Integer shopblance,// 充值购物余额
			Integer blance,// 充值话费
			Integer count//充值短信
	        ) throws Exception {
		// 返回参数
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		

		PrintWriter out = response.getWriter();
		String usernamesys = (String) request.getSession().getAttribute("useracc");
	
		if (id == null) {
			out.print("浏览器在点击的过程中发现丢失了用户信息，请您刷新重试！");
			return;
		} else {
			//充值系统费
//			if(linebalance != null) {
//				
//			}
			
			//设置透支额度
//			if(overdraft != null) {
//				
//			}
			
			//充值话费
//			if(blance != null) {
//				
//			}
			
			//购物余额充值
			if(shopblance != null) {
				Users users = usersService.selectByPrimaryKey(id);
				if(users.getShopblance() == null) {
					users.setShopblance(0);
				}
				users.setShopblance(users.getShopblance()+shopblance);
				int sCode = usersService.updateByPrimaryKeySelective(users);
			}
			//短信充值
			if(count !=null) {
				Smsconfigs smsconfigs = smsconfigsService.selectByAgentId(id);
				if (smsconfigs.getCount() == null) {
					smsconfigs.setCount(0);
				}
				smsconfigs.setCount(smsconfigs.getCount() + count);
				int codes = smsconfigsService.updateByPrimaryKeySelective(smsconfigs);
			}
//			if (code == 1) {
//				logsService.insert(CommContent.TYPE_UPDATE, "成功更新了" + record.getUsername() + "的账号信息", usernamesys,
//						true, IP.getIpAddr(request));
//				out.print(SUCCESS);
//			} else {
//				logsService.insert(CommContent.TYPE_UPDATE, "更新" + record.getUsername() + "的账号信息时失败了", usernamesys,
//						true, IP.getIpAddr(request));
//				out.print("充值失败，请您重试");
//			}
			
		}
	}

}
